﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeGenerator.Core
{
    public class TableInfo
    {
        public string Name{ get; set; }
        public string Comments { get; set; }
        public List<ColumnInfo> ColumnList { get; set; }
        public bool HasPirmaryKey
        {
            get
            {
                foreach (ColumnInfo current in this.ColumnList)
                {
                    if (current.IsPrimaryKey=="是" || current.AutoIncrement=="是")
                    {
                        return true;
                    }
                }
                return false;
            }
        }
        public List<ColumnInfo> PrimaryKeyColumns
        {
            get
            {
                List<ColumnInfo> list = new List<ColumnInfo>();
                foreach (ColumnInfo current in this.ColumnList)
                {
                    if (current.IsPrimaryKey=="是" || current.AutoIncrement =="是")
                    {
                        list.Add(current);
                    }
                }
                return list;
            }
        }

        /// <summary>
        /// 过滤掉表前缀的名称
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string FilterName(string str)
        {
            string result = this.Name;
            string[] prefixs = str.Split(',');
            foreach (var prefix in prefixs)
            {
                if (!string.IsNullOrEmpty(prefix) && this.Name.Contains(prefix))
                {
                    result = this.Name.Replace(prefix, "");
                    break;
                }
            }

            return result;
        }
    }
}
